package com.guojian.jdbc.sql;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.util.Assert;

import com.guojian.jdbc.DBType;

/**
 * 
 * sql解析器工厂
 * 
 * @author GuoJian
 *
 */
public class SqlParserFactory {

	//private static final MysqlParser mysqlParser = new MysqlParser();

	//private static final SqlServerParser sqlServerParser = new SqlServerParser();

	//private static final OracleParser oracleParser = new OracleParser();

	private static final Map<DBType, SqlParser> SQL_PARSER_MAP = new ConcurrentHashMap<>();

	static {
		SQL_PARSER_MAP.put(DBType.mysql, new MysqlParser());
		SQL_PARSER_MAP.put(DBType.sqlserver, new SqlServerParser());
		SQL_PARSER_MAP.put(DBType.oracle, new OracleParser());
	}

	public static synchronized SqlParser getMysqlParser() {

		/*if (mysqlParser == null) {
			mysqlParser = new MysqlParser();
		}*/

		return SQL_PARSER_MAP.get(DBType.mysql);
	}

	public static synchronized SqlParser getSqlServerParser() {

		/*if (sqlServerParser == null) {
			sqlServerParser = new SqlServerParser();
		}*/

		return SQL_PARSER_MAP.get(DBType.sqlserver);
	}

	public static synchronized SqlParser getOracleParser() {

		/*	if (oracleParser == null) {
				oracleParser = new OracleParser();
			}*/

		return SQL_PARSER_MAP.get(DBType.oracle);
	}

	public static SqlParser getSqlParser(DBType dbType) {
		SqlParser sqlParser = SQL_PARSER_MAP.get(dbType);
		Assert.notNull(sqlParser, "不支持当前数据库类型: " + dbType);
		return sqlParser;
	}

	public static void main(String[] args) {
		System.out.println(SqlParserFactory.getMysqlParser());
		System.out.println(SqlParserFactory.getMysqlParser());
		System.out.println(SqlParserFactory.getMysqlParser());

		System.out.println(DBType.sqlserver);
	}

}
